import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Experiment from '../components/Experiment.vue'
import Userinfo from '../components/users/Userinfo.vue'
import Exp from '../components/users/Exp'
import Users from '../components/admin/Users'
import CreateUser from '../components/admin/CreateUser'
import SearchGroup from '../components/admin/SearchGroup'
import ClassInfo from '../components/teacher/ClassInfo'
import AddClass from '../components/teacher/AddClass'
import StuToClass from '../components/teacher/StuToClass'
import AddTest from '../components/teacher/AddTest'
import MyTestInfo from '../components/teacher/MyTestInfo'
import FindTest from '../components/teacher/FindTest'
import AddSet from '../components/teacher/AddSet'
import FindSet from '../components/teacher/FindSet'
import StuDetails from '../components/teacher/StuDetails'
import SetDetails from '../components/teacher/SetDetails'
import Homework from '../components/teacher/Homework'
import CheckTest from '../components/teacher/homework/CheckTest'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/Experiment',
    children: [
      { path: '/Experiment', component: Experiment },
      { path: '/Exp', component: Exp },
      { path: '/Userinfo', component: Userinfo },
      { path: '/Users', component: Users },
      { path: '/CreateUser', component: CreateUser },
      { path: '/SearchGroup', component: SearchGroup },
      { path: '/ClassInfo', component: ClassInfo },
      { path: '/AddClass', component: AddClass },
      { path: '/StuToClass', component: StuToClass },
      { path: '/AddTest', component: AddTest },
      { path: '/AddSet', component: AddSet },
      { path: '/MyTestInfo', component: MyTestInfo },
      { path: '/FindTest', component: FindTest },
      { path: '/FindSet', component: FindSet },
      { path: '/StuDetails', component: StuDetails},
      { path: '/SetDetails', component: SetDetails},
      { path: '/Homework', component: Homework},
      { path: '/CheckTest', component: CheckTest}
    ]
  }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫

router.beforeEach((to, from, next) => {
  const strtoken = localStorage.getItem('token')
  if (strtoken) {
    next()
  } else {
    if (to.path === '/login') {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
